package com.example.controller;

import com.example.pojo.Result;
import com.example.pojo.changeModelPojo;
import com.example.pojo.fixtureTechConditionPojo;
import com.example.service.changeModelService;
import com.example.service.fixtureTechConditionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:ChangModelController
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/7/17 14:37
 * @Version 1.0
 */
@RestController
@Slf4j
public class ChangModelController {
    @Autowired
    private changeModelService changeModelService;
    @Value("${file.upload-dir}")
    private String saveDir;
    //插入一行新的数据
    @PostMapping("/insertChangeModel")
    public Result insertChangeModel(@RequestBody changeModelPojo changModel){
        changeModelService.insertOne(changModel);
        return Result.success();
    }
    //依据用户输入的作业指导书类型，进行查询
    @PostMapping("/inquiryType")
    public Result inquiryType(@RequestBody String type){
        List<changeModelPojo> changeModelList=changeModelService.inquiryByOneField("bookType",type);
        return Result.success(changeModelList);
    }
    //修改一行数据，修改的是表头数据
    @PostMapping("/editChangeModelTable")
    public Result editChangeModelTable(@RequestBody changeModelPojo changeModel){
        changeModelService.editChangeModelTable(changeModel);
        return Result.success();
    }
    //修改一个int类型的字段，现在用在修改标记位上
    @PostMapping("/editChangeModelFieldInt")
    public Result editChangeModelFieldInt(@RequestBody Map<String, Object> parameters){
        changeModelService.editFieldInt(parameters);
        return Result.success();
    }
    //修改一个字符串类型的字段，现在用在修改作业指导书上
    @PostMapping("/editChangeModelField")
    public Result editChangeModelField(@RequestBody Map<String, Object> parameters){
        String filed=changeModelService.editChangeModelField(parameters);
        return Result.success(filed);
    }
    //上传图片的函数(新页面上线后可删除)
    @PostMapping("/editChangeModelPicture")
    public Result editChangeModelPicture(@RequestParam("file") MultipartFile file, @RequestParam Integer id, @RequestParam String fieldName, @RequestParam String fieldVal){
        //上传照片的接口
        String allName = changeModelService.editChangeModelPicture(file,id,fieldName,fieldVal);
        return Result.success(allName);
    }

    //读取文件，返回给前端，用作保存数据用
    @GetMapping("/changeModelDownload")
    public Result changeModelDownload(@RequestParam("fileName") String fileName){
        byte[] file= changeModelService.readFile(fileName);
        return Result.success(file);
    }
    //删除文件,用在图片上
    @PostMapping("/changeModelPictureRemove")
    public Result changeModelPictureRemove(@RequestBody Map<String, Object> parameters) {
        String fileName=changeModelService.oilPictureRemove(parameters);
        return Result.success(fileName);
    }
    /*新页面上用到的函数*/
    //上传图片的函数（新页面用）
    @PostMapping("/editChangeModelPictureNew")
    public Result editChangeModelPictureNew(@RequestParam("file") MultipartFile file, @RequestParam Integer id, @RequestParam String fieldName, @RequestParam String fieldVal,@RequestParam String fileName){
        String allName = changeModelService.editChangeModelPictureNew(file,id,fieldName,fieldVal,fileName);
        return Result.success(fieldVal);
    }
    //删除文件用的函数
    @PostMapping("/changeModeNewPictureRemove")
    public Result changeModeNewPictureRemove(@RequestBody Map<String, Object> parameters) {
        String fileName=changeModelService.PictureRemove(parameters);
        return Result.success(fileName);
    }

    //调用工装技术条件service层，查询不重复的工装图纸编号
    @Autowired
    private fixtureTechConditionService fixtureTechConditionService;
    @PostMapping("/queryDrawingNumberChangeModelNew")
    public Result queryDrawingNumberChangeModelNew() {
        List<fixtureTechConditionPojo> pojo = fixtureTechConditionService.queryDrawingNumber();
        return Result.success(pojo);
    }
    //修改员工的信息，操作员工
    @PostMapping("/editOperatorChangeModelNew")
    public Result editOperatorChangeModelNew(
            @RequestParam(value = "file",required = false) MultipartFile file,
            @RequestParam(required = false) String fieldName,
            @RequestParam(required = false) String fieldVal,
            @RequestParam(required = false) String fileName,
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) int type,
            @RequestParam(required = false) String oldFileName
    ){
        //新增一个文件
        if (type==0){
            //把文件插入
            try {
                if (!file.isEmpty()) {
                    String filePath = saveDir + fileName;
                    File dest = new File(filePath);
                    // 将文件保存到服务器上的指定位置
                    file.transferTo(dest);
                    System.out.println("保存成功");
                    //向后端修改数据表，调用根据id，修改一个字段
                    // 创建Map实例
                    Map<String, Object> map = new HashMap<>();
                    map.put("id",id);
                    map.put("filedName",fieldName);
                    map.put("filedVal",fieldVal);
                    changeModelService.editOneFiledById(map);
                    return Result.success("修改成功");
                }
                else {
                    return Result.success("没有收到文件，请检查");
                }
            }
            catch (IOException e) {
                return Result.success("文件保存出错，请联系管理员");
            }
        }
        //删除老文件，使用新文件替换老文件
        else if (type==1){
            //1.删除老文件
            String filePath =saveDir + oldFileName;
            // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
            Path path = Paths.get(filePath);
            try {
                Files.delete(path);
            }
            catch (IOException e) {
                System.err.println("删除文件时出错: " + e.getMessage());
            }
            //2.删除文件后，修改数据表中存储文件路径的字段
            //向后端修改数据表，调用根据id，修改一个字段
            // 创建Map实例
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("filedName",fieldName);
            map.put("filedVal",fieldVal);
            changeModelService.editOneFiledById(map);
            //3.插入新文件
            try {
                if (!file.isEmpty()) {
                    filePath = saveDir + fileName;
                    File dest = new File(filePath);
                    // 将文件保存到服务器上的指定位置
                    file.transferTo(dest);
                }
            }
            catch (IOException e) {
                System.out.println("文件删除出错");
            }
            return Result.success("数据修改成功");
        }
        //删除文件，并且修改数据
        else if (type==2){
            //1.删除老文件
            String filePath =saveDir + oldFileName;
            // 使用 Paths 类的静态方法 get 来根据给定的文件路径字符串 filePath 创建一个 Path 对象
            Path path = Paths.get(filePath);
            try {
                Files.delete(path);
            }
            catch (IOException e) {
                System.err.println("删除文件时出错: " + e.getMessage());
                //调用service层修改数据库信息
                //删除文件后，修改数据表中存储文件路径的字段
            }
            //2.向后端修改数据表，调用根据id，修改一个字段
            // 创建Map实例
            Map<String, Object> map = new HashMap<>();
            map.put("id",id);
            map.put("filedName",fieldName);
            map.put("filedVal",fieldVal);
            changeModelService.editOneFiledById(map);
            return Result.success("图片删除成功，数据修改成功");
        }
        return Result.success("类型不正确，数据未修改");
    }

    //根据id，修改一个低频作业指导书的字段
    @PostMapping("/editOneFiledByIdChangeModelNew")
    public Result editOneFiledByIdChangeModelNew(@RequestBody Map<String,Object> map){
        changeModelService.editOneFiledById(map);
        return Result.success("编辑成功");
    }
}
